//
//  RadioHelpIntroducedViewController.m
//  RainbowFM
//
//  Created by Kratos on 14/12/16.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "RadioHelpIntroducedViewController.h"
#import "PhotosViewController.h"

#import <SwipeView/SwipeView.h>

#import "NSString+Size.h"

#import "HelpModel.h"




@interface RadioHelpIntroducedViewController () <SwipeViewDataSource,SwipeViewDelegate>
@property (nonatomic , weak) IBOutlet UIScrollView *contentScrollView;
@property (nonatomic , weak) IBOutlet SwipeView *swipeView;
@property (nonatomic , weak) IBOutlet UITextView *contentTextView;
@property (nonatomic , assign) CGFloat textViewDH;

@property (nonatomic , strong) NSMutableArray *imagesArray;
@end

@implementation RadioHelpIntroducedViewController

-(void)awakeFromNib
{
    _imagesArray = [NSMutableArray array];
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _textViewDH = CGRectGetHeight(_contentTextView.bounds);
    
    RFMImageViewItem *imageItem = [[RFMImageViewItem alloc]init];
    imageItem.imagePath = _item.PICURL;
    imageItem.videoPath = _item.MEURL;
    imageItem.isav = NO;
    [_imagesArray addObject:imageItem];
    [_swipeView reloadData];
    
    [self setIntroducedText:_item.DETILDESC];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setIntroducedText:(NSString *)text
{
    if (text == nil) {
        text = @"";
    }
    
    CGSize size = [text calculateSize:CGSizeMake(CGRectGetWidth(self.view.bounds) - 8.f * 2, 1000) font:[UIFont systemFontOfSize:14]];
    
    CGFloat changeH = size.height > _textViewDH ? size.height - _textViewDH : 0;
    
    if (changeH > 0) {
        CGSize scrolSize = _contentScrollView.contentSize;
        scrolSize.height += changeH;
        [_contentScrollView setContentSize:scrolSize];
        
        CGRect textFrame = _contentTextView.frame;
        textFrame.size.height = _textViewDH + changeH;
        _contentTextView.frame = textFrame;
    }
    
    [_contentTextView setText:text];
}

#pragma mark -SwipeViewDataSource
- (NSInteger)numberOfItemsInSwipeView:(SwipeView *)swipeView
{
    return [_imagesArray count];
}

- (UIView *)swipeView:(SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    RFMImageView *showImageView = (RFMImageView *)view;
    if (!showImageView) {
        showImageView = [[RFMImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_swipeView.bounds), CGRectGetHeight(_swipeView.bounds))];
        
        view = showImageView;
    }
    
    [showImageView setItem:_imagesArray[index]];
    
    return view;
}

#pragma mark - SwipeViewDelegate
- (void)swipeViewCurrentItemIndexDidChange:(SwipeView *)swipeView
{
    
}

- (void)swipeView:(SwipeView *)swipeView didSelectItemAtIndex:(NSInteger)index
{
    if (_item) {
        PhotosViewController *photosViewController = [PhotosViewController new];
        [photosViewController setImageUrls:_item.PICURL ? @[_item.PICURL] : nil];
        [self.navigationController pushViewController:photosViewController animated:YES];
    }
    
}



#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
